﻿#region Copyright Jonathan Quiros

//	
// All rights are reserved. Reproduction or transmission in whole or in 
// part, in any form or by any means, electronic, mechanical or otherwise, 
// is prohibited without the prior written consent of the copyright owner.
//
// Filename: ListController.cs
//

#endregion

using System.Linq;
using System.Collections.Specialized;
using System.Web.Mvc;
using Novagoal.Logic;
using Ids.Utils;

namespace Novagoal.Website.WebApp.Controllers
{
    public class ListController : BaseController
    {
        #region Properties

        public ListController(UsersLogic usersLogic) : base(usersLogic)
        {
        }

        /// <summary>
        /// Gets the sort field.
        /// </summary>
        /// <value>The sort field.</value>
        public string SortField
        {
            get
            {
                return Request.Params[RouteValues.SortField.ToStringValue()];
            }
        }

        /// <summary>
        /// Gets the sort dir.
        /// </summary>
        /// <value>The sort dir.</value>
        public string SortDirection
        {
            get
            {
                return Request.Params[RouteValues.SortDirection.ToStringValue()];
            }
        }

        /// <summary>
        /// Gets the page number.
        /// </summary>
        /// <value>The page number.</value>
        public int PageNumber
        {
            get
            {
                int pageNumber;
                if (int.TryParse(Request.Params[RouteValues.PageNumber.ToStringValue()], out pageNumber))
                    return pageNumber;

                return 1;
            }
        }


        /// <summary>
        /// Gets the page size.
        /// </summary>
        /// <value>The page size.</value>
        public int PageSize
        {
            get
            {
                int pageSize;
                PagingBlock = int.TryParse(Request.Params[RouteValues.PageSize.ToStringValue()], out pageSize)
                                  ? pageSize
                                  : ApplicationSettings.PageSize;

                return PagingBlock;
            }
        }
        #endregion

        #region Private Members
        public static int PagingBlock;
        #endregion

        #region Public Methods
        /// <summary>
        /// Redirects to the current action using as QueryString the parameters from the form collection
        /// </summary>
        /// <param name="formCollection">The form collection.</param>
        /// <returns></returns>
        public RedirectResult Redirect(FormCollection formCollection)
        {
            return Redirect(Request.Url.AbsolutePath, formCollection);
        }

        /// <summary>
        /// Redirects to a url using as QueryString the parameters from the form collection
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="formCollection">The form collection.</param>
        /// <returns></returns>
        public RedirectResult Redirect(string url, FormCollection formCollection)
        {
            NameValueCollection queryCollection = Request.QueryString;
            const string strParam = "{0}={1}&";

            string strRequestQuery = string.Empty;
            foreach (string key in Request.QueryString.Keys)
            {
                if (formCollection.AllKeys.Contains(key))
                {
                    string value = formCollection[key];
                    if (!string.IsNullOrEmpty(value))
                        strRequestQuery += string.Format(strParam, key, value);
                }
                else
                    strRequestQuery += string.Format(strParam, key, queryCollection[key]);
            }

            foreach (string key in formCollection.Keys)
            {
                if (!queryCollection.AllKeys.Contains(key))
                {
                    string value = formCollection[key];
                    if (!string.IsNullOrEmpty(value))
                        strRequestQuery += string.Format(strParam, key, value);
                }
            }

            char chrJoin = url.Contains("?") ? '&' : '?';
            return Redirect(url + chrJoin + strRequestQuery);
        }
        #endregion
    }

    public enum RouteValues
    {
        [Value("sort_direction")]
        SortDirection,
        [Value("page_number")]
        PageNumber,
        [Value("sort_field")]
        SortField,
        [Value("action")]
        Action,
        [Value("page_size")]
        PageSize
    }
}
